package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.AttendanceMapper;
import com.itheima.pojo.Attendance;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Rest;
import com.itheima.pojo.User;
import com.itheima.pojo.WeeklyReport;
import com.itheima.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public void checkIn(Integer userId) {
        // 查询当天的考勤记录
        LocalDate today=LocalDate.now();
        Attendance attendance = new Attendance();
            // 如果当天没有考勤记录，创建新的考勤记录
            attendance = new Attendance();
            attendance.setMemberId(userId);
            attendance.setDate(today);
            attendance.setCheckIn(LocalTime.now());
            attendance.setStatus("正常"); // 默认考勤状态为正常
            attendanceMapper.insert(attendance); // 插入新的考勤记录

    }


    @Override
    public void checkOut(Integer memberId) {
        System.out.println(12);
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);  // 根据 member_id 查询
        queryWrapper.last("LIMIT 1");  // 添加 LIMIT 1 限制返回一条记录
        Attendance attendance = attendanceMapper.selectOne(queryWrapper);  // 返回符合条件的第一条记录
        System.out.println(21);
        if (attendance != null) {
            attendance.setCheckOut(LocalTime.now());
            attendance.setNickname(null);
            // 根据签到和签退时间计算考勤状态
            calculateStatus(attendance);
            attendanceMapper.updateById(attendance); // 更新签退时间和考勤状态
        }
    }

    public Attendance select(){
        Attendance attendance=new Attendance();
        return attendance;
    }


    @Override
    public PageBean<Attendance> getDaysReportsForMember(Integer pageNum, Integer pageSize, Integer userId) {
        // 查询指定学生的所有考勤记录
        PageBean<Attendance> pb = new PageBean<>();

        // 开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        // 使用自定义 SQL 查询，联接 User 表来获取 nickname
        List<Attendance> as = attendanceMapper.selectAttendanceWithNickname(userId);

        // 转换分页
        Page<Attendance> page = (Page<Attendance>) as;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());

        return pb;
    }




    private void calculateStatus(Attendance attendance) {
        if (attendance.getCheckIn().isAfter(LocalTime.of(9, 0))) {
            attendance.setStatus("迟到");
        }
        if (attendance.getCheckOut().isBefore(LocalTime.of(17, 0))) {
            attendance.setStatus(attendance.getStatus()+"早退");
        }
    }
}
